﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TinyCinema.Data;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using TinyCinema.Bussiness;

namespace TinyCinema.Bussiness.Login
{
    public class Employee: DependencyObject
    {
        public static DependencyProperty EmpProperty = DependencyProperty.Register("Emp", typeof(Data.Employee), typeof(Employee));
        public Data.Employee Emp
        {
            get { return (Data.Employee)GetValue(EmpProperty); }
            set
            {
                SetValue(EmpProperty, value);
            }
        }
        public static DependencyProperty OldPassProperty = DependencyProperty.Register("OldPass", typeof(string), typeof(Employee));
        public string OldPass
        {
            get { return (string)GetValue(OldPassProperty); }
            set
            {
                SetValue(OldPassProperty, value);
            }
        }
        public static DependencyProperty ConfirmPassProperty = DependencyProperty.Register("ConfirmPass", typeof(string), typeof(Employee));
        public string ConfirmPass
        {
            get { return (string)GetValue(ConfirmPassProperty); }
            set
            {
                SetValue(ConfirmPassProperty, value);
            }
        }
        public static DependencyProperty NewPassProperty = DependencyProperty.Register("NewPass", typeof(string), typeof(Employee));
        public string NewPass
        {
            get { return (string)GetValue(NewPassProperty); }
            set
            {
                SetValue(NewPassProperty, value);
            }
        }
        public Messenger Messenger { get; set; }
        public ICommand ChangePasswordCommand { get; set; }
        public Employee()
        {
            Messenger = new Bussiness.Messenger();
            ChangePasswordCommand = new RelayCommand<object>(
                (s) => true,
                (s) =>
                {
                    if (NewPass.Length<6)
                    {
                        Messenger.Send(new Message.PasswordLenghtNotEnough());
                        return;
                    }
                    if (NewPass!=ConfirmPass)
                    {
                        Messenger.Send(new Message.ConfirmPasswordNotMatch());
                        return;
                    }
                    if (Data.Encryption.CreateMD5Hash(OldPass) != Emp.Password)
                    {
                        Messenger.Send(new Message.OldPassIncorrect());
                        return;
                    }
                    Emp.Password = NewPass;
                    Messenger.Send(new Message.PasswordChanged());
                }
            );
        }
        public bool Login(string ID,string Password)
        {
            try
            {
                Emp = new Data.Employee(ID);                
                if (Data.Encryption.CreateMD5Hash(Password) != Emp.Password) return false;                
            }
            catch
            {
                return false;
            }
            return true;
        }
    }
}
